package org.kugua.utils;

import org.kugua.common.Constants;
import org.kugua.context.CaptchaContext;
import org.springframework.core.io.ClassPathResource;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;

public class ImageUtils {

    public static boolean isSupportType(String name) {
        return isSupportType(name, Constants.Image.JPG, Constants.Image.PNG, Constants.Image.JPEG);
    }

    /**
     * 是否支持文件后缀
     *
     * @param name 文件名
     * @param imageType 文件类型
     * @return boolean
     */
    public static boolean isSupportType(String name, String... imageType) {
        if (imageType == null || imageType.length == 0) {
            return false;
        }
        String suffix = name.split("\\.", 2)[1].toUpperCase();
        return Arrays.asList(imageType).contains(suffix);
    }

    /**
     * 图片缩放处理
     *
     * @param bufferedImage 原始图像
     * @param scale 缩放比例
     * @return BufferedImage
     */
    public static BufferedImage scaleImage(BufferedImage bufferedImage, double scale) {
        // 缩放处理
        int originalW = bufferedImage.getWidth();
        int originalH = bufferedImage.getHeight();
        int scaledWidth = (int) (originalW * scale);
        int scaledHeight = (int) (originalH * scale);
        Image scaledInstance = bufferedImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_FAST);
        BufferedImage scaledImage = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = scaledImage.createGraphics();
        g2d.drawImage(scaledInstance, 0, 0, null);
        g2d.dispose();
        return scaledImage;
    }

    /**
     * 图片缩放
     *
     * @param bufferedImage 原始图像
     * @param scaleW 缩放后的宽度
     * @param scaleH 缩放后的长度
     * @return BufferedImage
     */
    public static BufferedImage scaleImage(BufferedImage bufferedImage, int scaleW, int scaleH) {
        Image scaledInstance = bufferedImage.getScaledInstance(scaleW, scaleH, Image.SCALE_FAST);
        BufferedImage bi = new BufferedImage(scaleW, scaleH, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = bi.createGraphics();
        graphics.drawImage(scaledInstance, 0, 0, null);
        graphics.dispose();
        return bi;
    }

    /**
     * 裁剪图片
     *
     * @param oriImage 原始图片
     * @param templateImage 模板图片
     * @param xPos x位置
     * @param yPos y位置
     * @return BufferedImage
     */
    public static BufferedImage cropImage(BufferedImage oriImage, BufferedImage templateImage, int xPos, int yPos) {
        // 模板图像矩阵
        int bw = templateImage.getWidth(null);
        int bh = templateImage.getHeight(null);
        BufferedImage targetImage = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB);
        for (int y = 0; y < bh; y++) {
            for (int x = 0; x < bw; x++) {
                int rgb = templateImage.getRGB(x, y);
                int alpha = (rgb >> 24) & 0xff;
                // 透明度大于100才处理
                if (alpha > 100) {
                    int bgRgb = oriImage.getRGB(xPos + x, yPos + y);
                    targetImage.setRGB(x, y, bgRgb);
                }
            }

        }
        return targetImage;
    }
}
